import screen from '@ohos.screen';
import display from '@ohos.display';
import window from '@ohos.window';


export async function getScreenSize(){
    let width = 1280;
//    let cfg = await globalThis.appContext.resourceManager.getConfiguration();
//    console.info('getScreenSize direction' + JSON.stringify(cfg.direction))
//    let direction = cfg.direction;
    // 应用所在window尺寸
    // @ts-ignore
    let win = await window.getTopWindow(globalThis.appContext);
    // @ts-ignore
    let prop = await win.getProperties();
    let rect = prop.windowRect;
    let height = rect.height*1280/rect.width;

    let lpi = parseFloat((rect.width/1280).toFixed(3));

    let dis = await display.getDefaultDisplay();

    let dpi = parseFloat((dis.densityDPI/160).toFixed(3));

    // 屏幕尺寸
//    let screenData = await screen.getAllScreens();
//    let size = screenData[0].supportedModeInfo[0];
//    let height = size.height*1280/size.width;
//    console.info('getScreenSize direction' + JSON.stringify(size))
//    console.info('getScreenSize width:' + width)
//    console.info('getScreenSize height:' + height)
    let res = { width:width, height: Math.floor(height), pageW:rect.width, pageH:rect.height, dpi:dpi, lpi:lpi};
    console.info('getScreenSize res:' + JSON.stringify(res))
    return res;
}

export function objType(obj){
    return Object.prototype.toString.call(obj).replace(/\[object /, '').replace(/\]/, '').toLowerCase();
}

export function getTimeStr(s:number){
    console.info('getTimeStr s:' + s);
    let h = '';
    let m = '';
    let sec = ''
    let seconds = Math.floor(s/1000);
    console.info('getTimeStr seconds:' + seconds);
    let hour = Math.floor(seconds/3600);
    console.info('getTimeStr hour:' + hour);
    seconds = seconds - hour*3600
    if(hour>0){
        h = `${hour}:`
    }
    let min = Math.floor(seconds/60);
    console.info('getTimeStr min:' + min);
    seconds = seconds - min*60;

    m = String(min).padStart(2, '0') + ':';
    sec = String(seconds).padStart(2, '0');

    return `${h}${m}${sec}`;
}

export function getVideoSize(pageW:number,pageH:number,videoW:number,videoH:number,type:string|number=6){
    let t = 6;
    let page_s = pageW/pageH;
    let video_s = videoW/videoH;

    let video_w = pageW;
    let video_h = pageH;

    switch(type){
        case 0:{ //4:3
            if(page_s > (4/3)){
                video_w = parseInt((pageH*4/3).toFixed(0))
            }else{
                video_h = parseInt((pageW*3/4).toFixed(0))
            }
            break;
        }
        case 1:{ //16:9
            if(page_s > (16/9)){
                video_w = parseInt((pageH*16/9).toFixed(0))
            }else{
                video_h = parseInt((pageW*9/16).toFixed(0))
            }
            break;
        }
        case 6:{
            if(page_s>video_s){
                video_w = parseInt((pageH*video_s).toFixed(0))
            }else{
                video_h = parseInt((pageW/video_s).toFixed(0))
            }
            break;
        }
    }
    return {width:video_w,height:video_h}
}

// 毫秒转日期
export function secToStr(sec){
    let time = new Date(sec);
    let year:any = time.getFullYear();
    let month:any = time.getMonth() + 1;
    let day:any = time.getDate();
    let h:any = time.getHours();
    let m:any = time.getMinutes();
    let s:any = time.getSeconds();

    month = String(month).padStart(2, '0');
    day = String(day).padStart(2, '0');
    h = String(h).padStart(2, '0');
    m = String(m).padStart(2, '0');
    s = String(s).padStart(2, '0');
    return `${year}-${month}-${day} ${h}:${m}:${s}`;
}


export function isVideoFile(name:string){
    if(name.includes('.')){
        let type = name.split('.').pop().toLocaleLowerCase()
        let types = 'mp4;mpeg-ts;webm;mkv'.toLocaleLowerCase();
        return types.includes(type);
    }
    return false;
}
